--行会主殿功能
--#include "data\config\guild\Guild.lua" once

GUILD_BOSS_UNDO	  	=	1				--未开始
GUILD_BOSS_DOING	=	2				--进行中
GUILD_BOSS_DONE		=	3				--已结束

GUILD_FUBEN_CUR_BOSS  =   1 			--副本当前BOSS

--[[
注册一些事件
]]
function ResisterGuildBossProc()
	--print("ResisterGuildBossProc...")
	for fubenIdx, fubenCfg in ipairs( GuildConfig.guildBoss ) do
		SetNeedReloginFuben( fubenCfg.fubenId, GlobalLuaConfig.reloginFuben.guildBossIdx, GetElemsForReLoginGuildBoss )
				--该副本是需要掉线恢复的
		SceneExitDispatcher.register( fubenCfg.sceneId, OnExitGuildBoss )						--注册离开行会BOSS副本后触发的事件
		FubenTimeoutDispatcher.register( fubenCfg.fubenId, OnGuildBossFubenTimeout )			--注册行会BOSS副本时间到后触发的事件
		SceneLogoutDispatcher.register( fubenCfg.sceneId, OnCommonFubenLogout )					--注册场景、副本中下线触发的事件
	end
end

function IsInGuildBossFuben( fubenId )
	for fubenIdx, fubenCfg in ipairs( GuildConfig.guildBoss ) do
		if fubenCfg.fubenId == fubenId then
			return true
		end
	end
	return false
end

--[[
获取掉线重登的配置和数据
]]
function GetElemsForReLoginGuildBoss( fubenPtr )
	local bossId 	= Fuben.getFbValue( fubenPtr, GUILD_FUBEN_CUR_BOSS )
	--print("GetElemsForReLoginGuildBoss, bossId="..bossId)
	local fubenCfg 	= GetGuildBossCfgByBossId( bossId )
	local InitFubenRightPanel 	= InitGuildBossPanel
	local loginLog   			= "guild boss login" 
	local loginLogOk			= "guild boss login ok" 
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

--[[
退出个人BOSS副本
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitGuildBoss( sysarg, sceneId, fubenId )
	--print("OnExitGuildBoss, sceneId="..sceneId..", fubenId="..fubenId)
	CloseActivityRightPanel(sysarg)				--关闭面板
	CloseCommonBossHurtRankPanel(sysarg, 0)		--关闭伤害排行榜面板
end

--[[
副本时间到
]]
function OnGuildBossFubenTimeout( fubenPtr )
	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )			--时间到的副本handle
	local guildId 		= Fuben.getGuildId( fubenHandle )
	--[[因为Fuben.setFubenTime( fubenHandle, 1 )会导致副本延迟1秒执行撤销，所以Fuben.setFubenTime( fubenHandle, 1 )后，再立即进行创建新副本
	nowFubenHandle可能不等于fubenHandle
	]]
	local guildBossSts, nowFubenHandle = GetGuildBossStsAndHandle( guildId )	--当前正在进行的副本handle
	--print("OnGuildBossFubenTimeout, guildId="..guildId..", fubenHandle="..fubenHandle..", nowFubenHandle="..nowFubenHandle)
	if nowFubenHandle ~= fubenHandle then
		return
	end
	SetGuildBossStsAndHandle( guildId, GUILD_BOSS_DONE, 0 )
	Guild.nofityGuildBossSts( guildId, GUILD_BOSS_DONE )			--通知行会成员活动结束	
	------------------------------有代码-----------------------------
end

--[[行会今日]]
function GetGuildBossDailyOpenTimes( guildId )
	local dVar = System.getDyanmicVar()
	if not dVar.guildBoss then
		dVar.guildBoss = {}
	end

	if not dVar.guildBoss[guildId] then
		dVar.guildBoss[guildId]		= {}
	end
	return dVar.guildBoss[guildId][3] or 0
end

function SetGuildBossDailyOpenTimes( guildId, newTimes )
	local dVar = System.getDyanmicVar()
	if not dVar.guildBoss then
		dVar.guildBoss = {}
	end

	if not dVar.guildBoss[guildId] then
		dVar.guildBoss[guildId]		= {}
	end

	dVar.guildBoss[guildId][3] = newTimes
end


function GetGuildBossStsAndHandle( guildId )
	local dVar = System.getDyanmicVar()
	if not dVar.guildBoss then
		dVar.guildBoss = {}
	end

	if not dVar.guildBoss[guildId] then
		dVar.guildBoss[guildId]		= {}
		dVar.guildBoss[guildId][1] 	= GUILD_BOSS_UNDO
		dVar.guildBoss[guildId][2] 	= 0
	end

	local guildBossSts 		= dVar.guildBoss[guildId][1] or GUILD_BOSS_UNDO
	local guildBossHandle 	= dVar.guildBoss[guildId][2] or 0
	return guildBossSts, guildBossHandle
end

function SetGuildBossStsAndHandle( guildId, newSts, newHandle )
	local dVar = System.getDyanmicVar()
	if not dVar.guildBoss then
		dVar.guildBoss = {}
	end

	if not dVar.guildBoss[guildId] then
		dVar.guildBoss[guildId]		= {}
		dVar.guildBoss[guildId][1] = GUILD_BOSS_UNDO
		dVar.guildBoss[guildId][2] = 0
	end

	dVar.guildBoss[guildId][1] = newSts
	dVar.guildBoss[guildId][2] = newHandle
end

--[[开启行会BOSS
开启对应的BOSS副本
]]
function OpenGuildBoss( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return false
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return false
	end


	local guildPos = Actor.getGuildPos(sysarg)
	if guildPos ~= smGuildLeader and guildPos ~=smGuildAssistLeader and guildPos ~= smGuildTangzhu then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0034,ttFlyTip)
		return
	end

	local bossCfg = GuildConfig.guildBoss[guildLevel]
	if not bossCfg then
		return false
	end

	if not CheckTimeValid(bossCfg.openTime) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildBoss003, ttFlyTip)
		return false
	end

	--计算当前时间到最后截止时间的时间差
	local year, month, day, hour, minute, second = nil, nil, nil, bossCfg.openTime[2][1], bossCfg.openTime[2][2], 0
	local endTime = { year, month, day, hour, minute, second }
	local lastFubenTime = GetDiffBetweenTwoTimes( nil, endTime )
	--print("OpenGuildBoss, lastFubenTime="..lastFubenTime)
	if lastFubenTime <= 5 then		--5秒之内没时间打了
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.GuildBoss003,ttFlyTip)
		return false
	end

	--print("OpenGuildBoss, guildId="..guildId..", guildLevel="..guildLevel)
	local guildBossSts,  fubenHandle = GetGuildBossStsAndHandle( guildId )
	--print("OpenGuildBoss, guildBossSts="..guildBossSts)
	if guildBossSts == GUILD_BOSS_DOING then			--正在进行中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0043,ttFlyTip)
		return false
	end

	local openTimes = GetGuildBossDailyOpenTimes( guildId )
	if openTimes >= #bossCfg.openConsume then				--行会BOSS次数
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildBoss005, ttFlyTip)
		return false
	end

	local nextOpenTime = openTimes + 1
	local needGuildCoin = bossCfg.openConsume[nextOpenTime]
	if not needGuildCoin then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildBoss005, ttFlyTip)
		return false
	end

	local hasGuildCoin = Actor.getGuildCoin(sysarg)
	--print(" OpenGuildBoss, nextOpenTime="..nextOpenTime..", needGuildCoin="..needGuildCoin..", hasGuildCoin="..hasGuildCoin)
	if needGuildCoin > hasGuildCoin then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0083, ttFlyTip)
		return false
	end

	if not Guild.changeGuildCoin(guildId, -needGuildCoin) then		--减少行会资金
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0083, ttFlyTip)
		return false
	end

	SetGuildBossDailyOpenTimes( guildId, nextOpenTime )
	--[[
	--print("OpenGuildBoss, guildId="..guildId..", guildLevel="..guildLevel)
	local guildBossSts,  fubenHandle = GetGuildBossStsAndHandle( guildId )
	--print("OpenGuildBoss, guildBossSts="..guildBossSts)
	if guildBossSts == GUILD_BOSS_DOING then			--正在进行中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0043,ttFlyTip)
		return
	elseif guildBossSts == GUILD_BOSS_DONE then			--今天已经结束了
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0042,ttFlyTip)
		return
	end
	]]

	--print("OpenGuildBoss, old fubenHandle="..fubenHandle)
	if fubenHandle > 0 then
		--Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0037,ttFlyTip)
		--return
		Fuben.setFubenTime( fubenHandle, 1 )				--手工开启，关闭正在进行的副本
	end
	
	fubenHandle = Fuben.createFuBen( bossCfg.fubenId )			--创建一个新的副本
	--print("OpenGuildBoss, guildId="..guildId..", fubenHandle="..fubenHandle)
	if fubenHandle <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0091, ttFlyTip)
		return false
	end

	SetGuildBossStsAndHandle( guildId, GUILD_BOSS_DOING, fubenHandle )
	Fuben.setGuildId( fubenHandle, guildId )				--设置副本相关的行会id
	--System.broadcastTipmsg( Lang.ScriptTips.GuildBoss001, ttScreenCenter+ttChatWindow )
	System.broadcastGuildMsg(guildId, Lang.ScriptTips.GuildBoss001)
	FreshGuildBoss( bossCfg, fubenHandle )					--刷出副本BOSS

	local fubenPtr = Fuben.getFubenPtr( fubenHandle )
	local bossId   = bossCfg.monsters[1].monsterId
	Fuben.setFbValue( fubenPtr, GUILD_FUBEN_CUR_BOSS, bossId )

	Fuben.setFubenTime( fubenHandle, lastFubenTime )		--调整副本时间
	Guild.nofityGuildBossSts( guildId, GUILD_BOSS_DOING, nextOpenTime )			--通知帮派成员活动开启
	return true
end

--[[进入对应的BOSS
进入对应的BOSS副本
]]
function EnterGuildBoss( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return
	end

	local bossCfg = GuildConfig.guildBoss[guildLevel]
	if not bossCfg then
		return
	end

	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_GuildFightBoss, 0 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		--return
	end

	if not Actor.canTelport(sysarg, bossCfg.sceneId) then  --是否允许场景传送
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0020,ttFlyTip)
		return
	end

	local guildBossSts, fubenHandle = GetGuildBossStsAndHandle( guildId )
	--print("EnterGuildBoss, guildBossSts="..guildBossSts..", fubenHandle="..fubenHandle)
	if guildBossSts == GUILD_BOSS_UNDO then				--尚未开始(首次进入开启副本，不需要这里)
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038,ttFlyTip)
		--return
	elseif guildBossSts == GUILD_BOSS_DONE then			--已经结束
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0042,ttFlyTip)
		return
	end

	--[[
	if fubenHandle <= 0 then				--没有副本，则创建副本
		if not OpenGuildBoss( sysarg ) then
			return
		end
		guildBossSts, fubenHandle = GetGuildBossStsAndHandle( guildId )
	end
	]]
	--print("EnterGuildBoss, guildId="..guildId..", fubenHandle="..fubenHandle)
	if fubenHandle <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038,ttFlyTip)
		return
	end

	if not Fuben.isFubenExist( fubenHandle ) then			--副本已经不存在（被撤销）了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildBoss004,ttFlyTip)
		return
	end

	local enterPos = bossCfg.enterPos
	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_GuildFightBoss, 0, 1 )
	if Actor.enterFuBen(sysarg, fubenHandle, bossCfg.sceneId, enterPos[1], enterPos[2]) then
		InitGuildBossPanel( sysarg, bossCfg )
	end
end

function InitGuildBossPanel( sysarg, bossCfg )
	local contStyle 	= 1
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_GuildBoss
	local title			= Lang.ScriptTips.GuildBossName
	OpenCommonStylePanel( sysarg, 1, activityId, subActivityId, title, bossCfg, contStyle, buttons  )
end

--[[
行会BOSS创建
]]
function OnGuildBossMob( bossPtr, bossId, sceneId, x, y )
	--print("OnGuildBossMob, bossId="..bossId)
	ClearCommonBossHurtRank( bossPtr )							--清除残留数据
	Actor.unregScriptCallback( bossPtr, "LoopGuildBoss" )
	Actor.regScriptTimer2S( bossPtr, 0, 3*1000, 3*1000, -1, "LoopGuildBoss", bossId)
end

--[[
定时器轮询处理
]]
function LoopGuildBoss( bossPtr, bossId)
	--print("LoopGuildBoss, bossId="..bossId)
	UpdateGuildBossRank( bossPtr )
end

function UpdateGuildBossRank( bossPtr )
	if bossPtr then
		local now = System.getCurrMiniTime()
		local bossLiveTime = Actor.getMonsterLiveTime( bossPtr )
		local lastTime = bossLiveTime - now
		if lastTime < 0 then
			lastTime = 0
		end

		local playerList = LuaHelp.getNearEntityList( bossPtr, 12, enActor)
		for i, player in ipairs(playerList or {}) do
			SendCommonBossHurtRankData( player, bossPtr, lastTime, nil )
		end
	end
end

--[[
行会怪物被击杀
]]
function OnGuildBossDie( bossPtr, sysarg, bossId, lastHitKiller )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		return
	end

	local guildLevel = System.getGuildbuildingLevel(guildId, gbtMainHall)
	if guildLevel < 1 then
		return
	end

	--local bossCfg = GuildConfig.guildBoss[guildLevel]
	local bossCfg = GetGuildBossCfgByBossId( bossId )
	if not bossCfg then
		return
	end

	--local sceneId 		= Actor.getSceneId( monster )
	local fubenPtr 		= Actor.getFubenPrt( bossPtr )
	local fubenHandle	= Fuben.getFubenHandle(fubenPtr) or 0
	local allBoss, killBoss, allMon, killMon = GetCommonBossMonsterNum6( fubenHandle, bossCfg.monsters )
	--print("OnGuildBossDie, allBoss="..allBoss..", killBoss="..killBoss..", allMon="..allMon..", killMon="..killMon)
	if killBoss >= allBoss and killMon >= allMon then
		OnGuildBossAllDie( fubenPtr, bossCfg)

		local killerId = Actor.getActorId( lastHitKiller )
		SendCommonBossHurtRankAward( bossPtr, killerId)		--发奖

		Actor.unregScriptCallback( bossPtr, "LoopGuildBoss" )			--清掉定时器
		UpdateGuildBossRank( bossPtr )									--广播一次排行榜数据	
		ClearCommonBossHurtRank( bossPtr )								--清空数据
		--Fuben.setFubenTime( fubenHandle, 1 )							--击杀完毕，关闭副本（BOSS没有掉落）
	end

	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		local activitySts = nil
		local contStyle 	= 1
		local buttons1 		= {1}
		local buttons2 		= {1}
		ChangeCommonStylePanel( sysarg, activitySts, contStyle, bossCfg, buttons1, buttons2 )
	end
end

--[[
行会BOSS没有被击杀成功，逃脱了
]]
function OnGuildBossTimeout( bossPtr, bossId)
	Actor.unregScriptCallback( bossPtr, "LoopGuildBoss" )				--清掉定时器
	ClearCommonBossHurtRank( bossPtr )									--清空数据
end

--[[
不需要，fubenId和场景Id是相同的
function GetGuildBossCfgBySceneId( sceneId )
	for bossIdx, bossCfg in ipairs( GuildConfig.guildBoss ) do
		if bossCfg.sceneId == sceneId then
			return bossCfg
		end
	end
	return nil
end
]]

function GetGuildBossCfgByBossId( bossId )
	for bossIdx, bossCfg in ipairs( GuildConfig.guildBoss ) do
		if bossCfg.monsters[1].monsterId == bossId then
			return bossCfg
		end
	end
	return nil
end

--[[
怪物全部死亡
]]
function OnGuildBossAllDie( fubenPtr, bossCfg )
	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
	local guildId 		= Fuben.getGuildId( fubenHandle )	
	--print("OnGuildBossAllDie, guildId="..guildId)
	--SetGuildBossStsAndHandle( guildId, GUILD_BOSS_DONE, 0 )
	SetGuildBossStsAndHandle( guildId, GUILD_BOSS_DONE, fubenHandle )		--只修改活动状态，副本handle不清0，以便下次开启时处理前一次副本
	Guild.nofityGuildBossSts( guildId, GUILD_BOSS_DONE )					--通知行会成员活动结束(此时副本还在)

	--[[
	local title 	=  Lang.ScriptTips.GuildBossMailTitle
	local content 	=  Lang.ScriptTips.GuildBossMailContent
	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		local actorId = Actor.getActorId( player )
		--print("OnGuildBossAllDie， actorId="..actorId)
		Awards.GiveToMailById( actorId, bossCfg.awards, tile, content, GameLog.clGuildBossKillAward, Lang.ScriptTips.GuildLog002 )
	end
	--System.broadcastTipmsg( Lang.ScriptTips.GuildBoss002, ttScreenCenter+ttChatWindow )
	--System.sendGuildMsg( guildId, smGuildCommon, Lang.ScriptTips.GuildBoss002, ttScreenCenter+ttChatWindow )
	]]
	System.broadcastGuildMsg(guildId, Lang.ScriptTips.GuildBoss002)
end

--[[
刷出行会BOSS副本类的怪物
]]
function FreshGuildBoss( bossCfg, fubenHandle )
	FreshCommonMonsters( bossCfg.monsters, fubenHandle )
end

function BroadGuildBossSts( fubenPtr )
	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		SendGuildBossSts( player )
	end
end

--下发行会BOSS活动状态
function SendGuildBossSts( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		return
	end
	local guildBossSts, fubenHandle = GetGuildBossStsAndHandle( guildId )
	local openTimes = GetGuildBossDailyOpenTimes( guildId ) or 0
	--print("SendGuildBossSts, guildBossSts="..guildBossSts)
	Guild.sendGuildBossSts( sysarg, guildBossSts, openTimes )
end

function NoticeGuildBossCloseRank( fubenPtr, bossId )
	--print("NoticeGuildBossClosePanel...")
	local playerList = LuaHelp.getFbActorList( fubenPtr )  --获取在线玩家对象列表
	for i, player in ipairs( playerList or {} ) do
		CloseCommonBossHurtRankPanel(player, 0)		--关闭面板，bossId无意义，客户端无需处理
	end
end

ResisterGuildBossProc()				--注册事件

